/**
 * 
 */
package com.appwx.sdk.actionbar;



import com.appwx.sdk.R;
import com.appwx.sdk.animation.ViewCompat;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

/**
 * @author xiajun
 *
 */
public class ParallaxScrollViewA extends ScrollView {

	
	private static final float DEFAULT_SCROLL_FACTOR = 0.6f;
    private float mScrollFactor = DEFAULT_SCROLL_FACTOR;

    private static final boolean PRE_HONEYCOMB = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
    private int mBackgroundResId;
    private View mBackgroundView;
    
    
	public ParallaxScrollViewA(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context,attrs,defStyle);
	}

	public ParallaxScrollViewA(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context,attrs,0);
	}

	public ParallaxScrollViewA(Context context) {
		super(context);
		initView(context,null,0);
	}

	/**
	 * 初始化View
	 * @param context     上下文
	 * @param attrs       自定义属性
	 * @param defStyle    默认样式(不使用默认就设置为0)
	 */
	private void initView(Context context, AttributeSet attrs, int defStyle){
		if(isInEditMode()) return;
		
		if(attrs != null){
			TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.ParallaxScrollViewA);
			
			mBackgroundResId = values.getResourceId(R.styleable.ParallaxScrollViewA_backgroundView, 0);
			
			mScrollFactor = values.getFloat(R.styleable.ParallaxScrollViewA_scrollFactor, DEFAULT_SCROLL_FACTOR);
			
			values.recycle();
		}
			setVerticalFadingEdgeEnabled(false);
		
	}
	
	public void setBackgroundView(int id){
		mBackgroundView = findViewById(id);
	}
	
	public void setBackgroundView(View view){
		mBackgroundView = view;
	}
	
	public void setScrollFactor(float scrollFactor){
		mScrollFactor = scrollFactor;
	}
	
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		//View加载后或发生变化时,调用此事方法调整布局
		super.onLayout(changed, l, t, r, b);
		if(changed){
			translateBackgroundView(getScrollY());
		}
		
	}
	
	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		//附加View到窗口中
		if(mBackgroundResId > 0 && mBackgroundView == null){
			mBackgroundView = findViewById(mBackgroundResId);
			mBackgroundResId = 0;
		}
	}
	
	@Override
	protected void onDetachedFromWindow() {
		//从窗口中分离出来时调用此方法
		if(PRE_HONEYCOMB && mBackgroundView != null){
			mBackgroundView.clearAnimation();
		}
		mBackgroundView = null;
		super.onDetachedFromWindow();
	}
	
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		translateBackgroundView(t);
	}
	
	@SuppressWarnings("unused")
	private void translateBackgroundView(int y){
		if(mBackgroundView != null){
			int translateY = (int) (y * mScrollFactor);
			//如果当前sdk<11
			if(PRE_HONEYCOMB){
				ViewCompat.wrap(mBackgroundView).setTranslationY(translateY);//设置移动距离
			}else{
				mBackgroundView.setTranslationY(translateY);
			}
			
		}
	}
	
	
	
}
